import UIKit

// 小人
class Man: Box {
    private var mCurrentImage1: UIImage!    // 当前图片1
    private var mCurrentImage2: UIImage!    // 当前图片2
    private var mGameBoard: UIView!         // 游戏区域
    private var mImageLeft1: UIImage!       // 左图片1
    private var mImageLeft2: UIImage!       // 左图片2
    private var mImageRight1: UIImage!      // 右图片1
    private var mImageRight2: UIImage!      // 右图片2
    private var mImageUp1: UIImage!         // 上图片1
    private var mImageUp2: UIImage!         // 上图片2
    private var mTimer: Timer!              // 定时器

    /**
     * 构造方法。
     * @param back      背后对象
     * @param drawer    绘图者
     * @param gameBoard 游戏区域
     * @param type      类型
     * @param x, y      左上角坐标
     */
    public init(_ back: BaseGameObject, drawer: CGContext, gameBoard: UIView, type: _Type, x: Int, y: Int) {
        super.init(back, drawer: drawer, type: type, x: x, y: y)

        // 初始化成员
        mCurrentImage1 = mImage
        mCurrentImage2 = mImage2
        mGameBoard = gameBoard

        // 加载所有图片
        var imageFileName = String(format: Const.IMAGE_FILE_NAME, type.rawValue * 10 + 2)
        mImageLeft1 = super.loadImage(imageFileName)

        imageFileName = String(format: Const.IMAGE_FILE_NAME, type.rawValue * 10 + 3)
        mImageLeft2 = super.loadImage(imageFileName)

        imageFileName = String(format: Const.IMAGE_FILE_NAME, type.rawValue * 10 + 4)
        mImageRight1 = super.loadImage(imageFileName)

        imageFileName = String(format: Const.IMAGE_FILE_NAME, type.rawValue * 10 + 5)
        mImageRight2 = super.loadImage(imageFileName)

        imageFileName = String(format: Const.IMAGE_FILE_NAME, type.rawValue * 10 + 6)
        mImageUp1 = super.loadImage(imageFileName)

        imageFileName = String(format: Const.IMAGE_FILE_NAME, type.rawValue * 10 + 7)
        mImageUp2 = super.loadImage(imageFileName)
        
        // 开始跳舞
        mTimer = Timer.scheduledTimer(timeInterval: Const.TIMER_INIT, target: self, selector: #selector(dancing), userInfo: nil, repeats: true)
    }

    /**
     * 跳舞。
     */
    @objc private func dancing() {
        if mCurrentImage == mCurrentImage1 {
            mCurrentImage = mCurrentImage2
        } else {
            mCurrentImage = mCurrentImage1
        }
        let rect = CGRect(x: mX * BaseGameObject.sSize, y: mY * BaseGameObject.sSize, width: BaseGameObject.sSize, height: BaseGameObject.sSize)
        mDrawer.draw(mCurrentImage.cgImage!, in: rect)
        mGameBoard.setNeedsDisplay()
    }

    /**
     * 移动。
     * @param direction     方向
     * @param moveResult    移动结果
     * @return 被推动的箱子
     */
    public override func move(_ direction: Int, moveResult: inout MoveResult) -> BaseGameObject! {
        var destX = mX, destY = mY  // 新位置的坐标

        // 根据方向，计算新位置的坐标和图片
        switch direction {
            case Const.TAG_BUTTON_DOWN:
                destY += 1
                mCurrentImage1 = mImage
                mCurrentImage2 = mImage2
            case Const.TAG_BUTTON_LEFT:
                destX -= 1
                mCurrentImage1 = mImageLeft1
                mCurrentImage2 = mImageLeft2
            case Const.TAG_BUTTON_RIGHT:
                destX += 1
                mCurrentImage1 = mImageRight1
                mCurrentImage2 = mImageRight2
            case Const.TAG_BUTTON_UP:
                destY -= 1
                mCurrentImage1 = mImageUp1
                mCurrentImage2 = mImageUp2
            default:
                break
        }

        // 重新设置图片
        mCurrentImage = mCurrentImage1

        // 先移动旁边的物体
        let box = Game.sBaseGameObjects[destY][destX].move(direction, moveResult: &moveResult)
        if moveResult == .NO_MOVE {
            return nil
        }

        // 移动小人
        _ = super.move(direction, moveResult: &moveResult)

        return box
    }
    
    /**
     * 停止跳舞。
     */
    public func stopDancing() {
        mTimer.invalidate()
    }
}
